package com.hzncc.flowable_diboot.controller.customer;

import com.diboot.core.controller.BaseCrudRestController;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hzncc.flowable_diboot.coderule.annotation.CodeRule;
import com.hzncc.flowable_diboot.dto.CustomerFollowRecordDTO;
import com.hzncc.flowable_diboot.entity.CustomerFollowRecord;
import com.hzncc.flowable_diboot.service.impl.CustomerFollowRecordServiceImpl;
import com.hzncc.flowable_diboot.util.ConstantUtil;
import com.hzncc.flowable_diboot.vo.CustomerFollowRecordDetailVO;
import com.hzncc.flowable_diboot.vo.CustomerFollowRecordListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户跟进记录 相关rest接口
 *
 * @author MyName
 * @version 1.0
 * @date 2024-05-21
 * Copyright © MyCorp
 */
@Slf4j
@RequestMapping("/customer-follow-record")
@BindPermission(name = "客户跟进记录")
@RestController
public class CustomerFollowRecordController extends BaseCrudRestController<CustomerFollowRecord> {

    @Autowired
    private CustomerFollowRecordServiceImpl customerFollowRecordService;

    /**
     * 查询资源对象的列表VO记录
     * <p>
     * url请求参数示例: ?fieldA=abc&pageSize=20&pageIndex=1&orderBy=id
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping()
    public JsonResult<List<CustomerFollowRecordListVO>> getListVOMapping(CustomerFollowRecordDTO queryDto, Pagination pagination) throws Exception {
        return super.getViewObjectList(queryDto, pagination, CustomerFollowRecordListVO.class);
    }

    /**
     * 根据id查询资源对象的详情VO
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult<CustomerFollowRecordDetailVO> getDetailVOMapping(@PathVariable("id") String id) throws Exception {
        CustomerFollowRecordDetailVO detailVO = customerFollowRecordService.getViewObject(id, CustomerFollowRecordDetailVO.class);
        return JsonResult.OK(detailVO);
    }


    /**
     * 创建资源对象数据
     *
     * @param customerFollowRecord
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping()
    public JsonResult<?> createModelMapping(@RequestBody CustomerFollowRecord customerFollowRecord) throws Exception {
        boolean success = customerFollowRecordService.createEntity(customerFollowRecord);
        log.debug("新建数据 客户跟进记录:{} {}", customerFollowRecord.getId(), success ? "成功" : "失败");
        return JsonResult.OK(customerFollowRecord.getId());
    }

    /**
     * 批量创建资源对象数据
     *
     * @param customerFollowRecordList
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_BATCH_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch")
    public JsonResult<?> batchCreateModelMapping(@RequestBody List<CustomerFollowRecord> customerFollowRecordList) throws Exception {
        boolean success = customerFollowRecordService.createEntities(customerFollowRecordList);
        log.debug("批量新建数据 客户跟进记录 {}", success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据id更新资源对象
     *
     * @param customerFollowRecord
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateModelMapping(@PathVariable("id") String id, @RequestBody CustomerFollowRecord customerFollowRecord) throws Exception {
        boolean success = customerFollowRecordService.updateEntity(customerFollowRecord);
        log.debug("更新数据 客户跟进记录:{} {}", id, success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteModelMapping(@PathVariable("id") String id) throws Exception {
        boolean success = customerFollowRecordService.deleteEntity(id);
        log.debug("删除数据 客户跟进记录:{} {}", id, success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 根据ids集合删除资源对象
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_BATCH_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch-delete")
    public JsonResult<?> deleteEntityMapping(@RequestBody List<String> ids) throws Exception {
        boolean success = customerFollowRecordService.deleteEntities(ids);
        log.debug("批量删除数据 客户跟进记录:{} {}", ids, success ? "成功" : "失败");
        return JsonResult.OK(success);
    }

    /**
     * 客户跟进
     *
     * @return
     * @author 陈广煜
     * @date 2024/5/21
     */
    @Log(operation = OperationCons.LABEL_WRITE)
    @BindPermission(name = OperationCons.LABEL_WRITE,code = OperationCons.CODE_WRITE)
    @PostMapping("/follow")
    @CodeRule(entity = "com.hzncc.flowable_diboot.entity.CustomerFollowRecord",ruleCode = ConstantUtil.CODE_RULE_CUSTOMER_FOLLOW)
    public JsonResult<?> follow(@RequestBody CustomerFollowRecord record) {
        return customerFollowRecordService.follow(record);
    }

    /**
     * 客户跟进
     *
     * @return
     * @author 陈广煜
     * @date 2024/5/21
     */
    @Log(operation = OperationCons.LABEL_WRITE)
    @BindPermission(name = OperationCons.LABEL_WRITE,code = OperationCons.CODE_WRITE)
    @PutMapping("/follow/{id}")
    public JsonResult<?> followEdit(@RequestBody CustomerFollowRecord record, @PathVariable("id") String id) {
        return customerFollowRecordService.followEdit(record,id);
    }
}